class Solution {
public:
    string tree2str(TreeNode* root) {
        if(root == NULL) 
            return "";
        string ans = to_string(root->val);
        if(root->right != NULL || root->left != NULL)
            ans += "(" + tree2str(root->left) + ")";
        if(root->right != NULL)
            ans += "(" + tree2str(root->right) + ")";
        return ans;
    }
};
